<?php
/**********************
**main.php
**Date Created: 4/13/2009
**Copyright 2009 Tomorrow Tech Industries
**Contact: RPITomorrowTech@gmail.com
**********************/

require_once("private/shared/form.inc");
require_once("private/top.inc");
require_once("private/login.inc");
require_once("private/arrays.inc");
require_once("private/dba.inc");
require_once("private/intellistrip.inc");

//Form actions
//Turn all devices on
define('ACTION_ALL_ON', 'actionAllOn');
//Turn all devices off
define('ACTION_ALL_OFF', 'actionAllOff');

//A button for form actions
function addActionButton($intellistrip, $label, $size, $actionName)
{
	//Build a form string so that if an error occurs during construction, bad output does not appear on screen
	$formStr = "";
	
	$formStr .= "<FORM name=\"actionForm\" action=\"main.php\" method=\"POST\">\n";
	
	//Only output a size key/value pair if specified
	if( $size )
	{
		$sizeStr = "size=\"" . $size . "\" ";
	}
	else
	{
		$sizeStr = "";
	}
	
	$rc = $intellistrip->getIntellistripID($stripId);
	if( TT_FAILED($rc) )
	{
		echo "Failed to get strip id";
		return $rc;
	}
	
	//Which action are we trying to perform?
	//Can we get rid of this and just use the button?
	$formStr .= "<INPUT name=\"" . FORM_ACTION . "\" type=\"hidden\" value=\"" . $actionName . "\"/>\n";
	//Which intellistrip we are operating
	$formStr .= "<INPUT name=\"" . FORM_INTELLISTRIP_ID . "\" type=\"hidden\" value=\"" . $stripId . "\"/>\n";
	//Submit button	
	$formStr .= "<INPUT name=\"Submit\" " . $sizeStr . "type=\"submit\" value=\"" . $label . "\" />\n";

	//And end the form
	$formStr .= "</FORM>";

	echo $formStr;
}

//A "up/down" bar in the table used for spacing
function verticalSpacer()
{
	echo "
		<DIV style=\"background-color:#CCCCCC\" align=\"center\">
			&nbsp;<BR /><BR /><BR /><BR />
		</DIV>
		";
}

//A "left/right" bar in the table used for spacing
function horizontalSpacer()
{
	echo "
		<DIV style=\"background-color:#CCCCCC\" align=\"center\">
		&nbsp;
		</DIV>
		";
}

//Section of table where a device icon is usually displayed
//If device is not occupying slot, an add device input should appear
function displayDeviceIcon($intellistrip, $device, $portId)
{
	echo "<DIV align=\"center\">";
	
	//If we have a device for this entry, display it
	if( $device && TT_SUCCEEDED($device->getName($name)) )
	{
        //Get the image associated with the device
        
        $rc = $device->getType($type);
		if( TT_FAILED($rc) )
		{
			echo "<DIV style=\"background-color:#FF0000\">";
			echo "<b>Failed to get device type</b>";
			echo "</DIV>";
			return $rc;
		}
        
        $q = "SELECT iconPath FROM DeviceTypes WHERE typeID = " . $type;
		$db = new DBaccess(); 
		$rc = $db->executeQuery($q, $result);
        $row = mysql_fetch_array($result);
		$iconPath = $row[0];
        if( TT_FAILED($rc) )
		{
			echo "<DIV style=\"background-color:#FF0000\">";
			echo "<b>Failed to get device icon</b>";
			echo "</DIV>";
			return $rc;
		}
        
		//Display on or off image depending on whether its on or not
		
		$rc = $intellistrip->getIntellistripID($stripId);
		if( TT_FAILED($rc) )
		{
			echo "<DIV style=\"background-color:#FF0000\">";
			echo "<b>Failed to get strip id</b>";
			echo "</DIV>";
			return $rc;
		}

		$rc = $intellistrip->getPortPower($portId, $powerLevel);
		if( TT_FAILED($rc) )
		{
			echo "<DIV style=\"background-color:#FF0000\">";
			echo "<b>Failed to get port power</b>";
			echo "</DIV>";
			return $rc;
		}
		//For now just display either full on or full off
		//off
		if( $powerLevel == 0 )
		{
			echo "<DIV style=\"background-color:#CCCCCC\">";
			//Setup an off image with a link that will turn in on
			echo "
				<A href=main.php?"
					//Off devices switch to on
					. FORM_INTELLISTRIP_PORT_STATE . "=100&"
					//Information to identify a unique port
					. FORM_INTELLISTRIP_ID . "=" . $stripId . "&"
					. FORM_INTELLISTRIP_PORT_ID . "=" . $portId
				. ">
					<IMG src=" . $iconPath . " width=64 height=64 border=\"0\">
				</A>";
			echo "</DIV>";
		}
		//On
		else
		{
			echo "<DIV style=\"background-color:#33CC00\">";
			echo "
				<A href=main.php?"
					//Off devices switch to on
					. FORM_INTELLISTRIP_PORT_STATE . "=0&"
					//Information to identify a unique port
					. FORM_INTELLISTRIP_ID . "=" . $stripId . "&"
					. FORM_INTELLISTRIP_PORT_ID . "=" . $portId
				. ">
					<IMG src=" . $iconPath . " width=64 height=64 border=\"0\">
				</A>\n";
			echo "</DIV>";
		}
	}
	//No device, display an add icon
	else
	{
		$formStr = "";
		
		$formStr .= "<FORM name=\"action\" action=\"adddevice.php\" method=\"POST\">\n";
		
		if( TT_FAILED($intellistrip->getIntellistripID($intellistripId)) )
		{
			$intellistripId = 0;
		}
		
		$label = 'Add device';
		
		//Describe the add
		//Strip id
		$formStr .= "<INPUT name=\"" . FORM_INTELLISTRIP_ID. "\" type=\"hidden\" value=\"" . $intellistripId . "\"/>\n";
		//Port id
		$formStr .= "<INPUT name=\"" . FORM_INTELLISTRIP_PORT_ID . "\" type=\"hidden\" value=\"" . $portId . "\"/>\n";
		//Submit button	
		$formStr .= "<INPUT name=\"Submit\" size=\"40\" type=\"submit\" value=\"" . $label . "\" />\n";

		//And end the form
		$formStr .= "</FORM>\n";

		echo $formStr;
	}

	echo "</DIV>";
	return TT_OK;
}

function displayDeviceDescription($intellistrip, $device, $portId)
{
	//Do we have a device?
	if( $device && TT_SUCCEEDED($device->getName($name)) )
	{
		if( TT_FAILED($intellistrip->getPortPower($portId, $powerLevel)) || TT_FAILED($intellistrip->getIntellistripId($intellistripId)) )
		{
			//echo "Failed: " . Intellistrip::getLastError() . "<BR>";
			echo "<DIV align=\"center\">\n";
			echo "<font color=\"#FF0000\" size=\"+2\"><b>?</b></font>";
			echo "</DIV>\n";
		}
		else
		{
			echo "<DIV align=\"center\">\n";
			//Provide a reconfiguration link
			echo "<A href=\"adddevice.php?" . FORM_INTELLISTRIP_ID . "=" . $intellistripId . "&" . FORM_INTELLISTRIP_PORT_ID . "=" . $portId . "\">";
			echo htmlspecialchars($name);
			echo "</A>";	
			
			echo ", power level: " . $powerLevel . "%";
			echo "</DIV>\n";
		}
	}
	//Otherwise leave area more or less blank
	else
	{
		echo "<DIV align=\"center\">\n";
		echo "-";
		echo "</DIV>\n";
	}
}

function displayIntellistrip($intellistrip)
{
	$rc = $intellistrip->getName($name);
	if( TT_FAILED($rc) )
	{
		return $rc;
	}

	$rc = $intellistrip->getDevices($devicesPortMapped);
	if( TT_FAILED($rc) )
	{
		return $rc;
	}
	

	echo "<H2>" . $name . "</H2>\n";
	
	//Start with empty list
	$devicesIndexed = array();
	
	//For now we only support direct mapping of devices to ports
	for( $i = 0; $i < INTELLISTRIP_NUMBER_PORTS; $i = $i + 1 )
	{
		//Default to no device
		$device = null;
		//TRy to match up a device
		foreach( $devicesPortMapped as $key => $value )
		{
			if( $value['port'] == $i + INTELLISTRIP_PORT_BASE )
			{
				$device = $value['device'];
				break;
			}
		}
		
		array_push($devicesIndexed, $device);
	}


	//TODO: add room name
	//echo "Family Room<BR />\n";
	echo "<TABLE width=\"423\" border=\"0\">";
		
	echo "
		  <TR>
			<TD width=\"55\">
				<DIV align=\"left\">
		";
					addActionButton($intellistrip, 'All On', '57', ACTION_ALL_ON);
					echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	echo "
				</DIV>
			</TD>
			<TD width=\"79\">
				<DIV align=\"center\">
				</DIV>
			</TD>
			<TD width=\"67\">
				<DIV align=\"center\">
				</DIV>
			</TD>
			<TD width=\"16\">
				<DIV align=\"center\">
					" . "5" . "
				</DIV></TD>
		  	<TD width=\"89\" bgcolor=\"#CCCCCC\">
		";
				//Device 5
				displayDeviceIcon($intellistrip, $devicesIndexed[4], 5);
		
	echo "
			</TD>
		  	<TD width=\"77\">
		";
				//Device 5
				displayDeviceDescription($intellistrip, $devicesIndexed[4], 5);
	echo "
			</TD>
		</TR>
		";
	echo "
		  <TR>
			<TD height=\"43\">
				<DIV align=\"left\">
				";
				addActionButton($intellistrip, 'All Off', '', ACTION_ALL_OFF);
	echo "				
				</DIV>
			</TD>
			<TD>
				<DIV align=\"center\">
				</DIV>
			</TD>
			<TD>
				<DIV align=\"center\">
				</DIV>
			</TD>
			<TD>
				<DIV align=\"center\">" . "4" . "
				</DIV>
			</TD>
		  	<TD bgcolor=\"#CCCCCC\">
		";
				//Device 4
				displayDeviceIcon($intellistrip, $devicesIndexed[3], 4);
	echo "				
			</TD>
			<TD>
		 ";
		 		//Device 4
		 		displayDeviceDescription($intellistrip, $devicesIndexed[3], 4);
	echo "
			</TD>
		</TR>
		";
	echo "
		<TR>
			<TD>
				<DIV align=\"left\">
		";
					//We use to have a preset button here, they were dropped from support for now
					//<input name=\"Button\" type=\"button\" value=\"Preset 1\" onClick=\"alert('Preset 1 excecuted.')\"/>
	echo "
				</DIV>
			</TD>
			<TD>
				<DIV align=\"center\">
					" . "1" . "
				</DIV>
			</TD>
			<TD>
				<DIV align=\"center\">
					" . "2" . "
				</DIV>
			</TD>
			<TD>
				<DIV align=\"center\">
					" . "3" . "
				</DIV>
			</TD>
			<TD>
		";
				horizontalSpacer();
	echo "
			</TD>
			<TD>
				<DIV align=\"center\">
				</DIV>
			</TD>
		  </TR>
		  ";
	echo "
		  <TR>
			<TD>
				<DIV align=\"left\">
		";
				//We use to have a preset button here, they were dropped from support for now
				//<input name=\"Button\" type=\"button\" value=\"Custom\" onClick=\"alert('Custom Preset now excecuted.')\"/>
	echo "
				</DIV>
			</TD>
		  	<TD bgcolor=\"#CCCCCC\">
		";
				//Device 1
				displayDeviceIcon($intellistrip, $devicesIndexed[0], 1);
	echo "			
			</TD>
		  	<TD bgcolor=\"#CCCCCC\">
		";
				//Device 2
				displayDeviceIcon($intellistrip, $devicesIndexed[1], 2);
	echo "
			</TD>
			<TD>
		";
				verticalSpacer();
	echo "
			</TD>
		  	<TD bgcolor=\"#CCCCCC\">
		";
				//Device 3
				displayDeviceIcon($intellistrip, $devicesIndexed[2], 3);
	echo "
			</TD>
			<TD>
		";
				//Device 3
				displayDeviceDescription($intellistrip, $devicesIndexed[2], 3);
	echo "
			</TD>
		  </TR>
		";
	echo "
		  <TR>
			<TD>
				<DIV align=\"left\">
				</DIV>
			</TD>
			<TD>
		";
				//Device 1
				displayDeviceDescription($intellistrip, $devicesIndexed[0], 1);
	echo "
			<TD>
		";
				//Device 2
				displayDeviceDescription($intellistrip, $devicesIndexed[1], 2);
	echo "
			</TD>
			<TD>
				<DIV align=\"center\"></DIV>
			</TD>
			<TD>
				<DIV align=\"center\"></DIV>
			</TD>
			<TD>
				<DIV align=\"center\"></DIV>
			</TD>
		  </TR>
		";
	echo "</TABLE>";

	return TT_OK;
}

function displayIntellistrips()
{
	if( TT_FAILED(Intellistrip::getIntellistrips($intellistrips)) )
	{
		echo "<font color=\"#FF0000\"><b>Failed to get intellistrips!</b></font><BR>";
		return;
	}
	if( sizeof($intellistrips) == 0 )
	{
		echo "
			>>>>>No Intellistrips Found<<<<<<BR />
			To register an Intellistrip that you have bought, click \"Add Strip\" above.
			<BR />
			<BR />
			------------------<BR />
			<BR />
			";
		return;
	}

	foreach( $intellistrips as $key => $value )
	{
		//TRansform strip ID into a full object
		$intellistrip = $value;
		if( TT_FAILED(displayIntellistrip($intellistrip)) )
		{
			echo "<font color=\"#FF0000\"><b>Failed to display intellistrip!</b></font><BR />";
		}	
	}
}

function updateSpecificPort()
{
	//Did we do an all on request?
	if( !isset($_GET[FORM_INTELLISTRIP_PORT_ID])
		|| !isset($_GET[FORM_INTELLISTRIP_ID])
		|| !isset($_GET[FORM_INTELLISTRIP_PORT_STATE])
		)
	{
		return TT_OK;
	}

	$stripId = $_GET[FORM_INTELLISTRIP_ID];
	$portId = $_GET[FORM_INTELLISTRIP_PORT_ID];
	$portState = $_GET[FORM_INTELLISTRIP_PORT_STATE];
	
	$intellistrip = new Intellistrip($stripId);
	$rc = $intellistrip->setPortPower($portId, $portState);
	if( TT_FAILED($rc) )
	{
		echo "<font color=\"#FF0000\"><b>Failed to set port power</b></font>";
		return $rc;
	}
	return TT_OK;
}

function updateAction()
{
	//Did we do an all on request?
	if( !isset($_POST[FORM_ACTION])
		|| !isset($_POST[FORM_INTELLISTRIP_ID]) 
		)
	{
		return TT_OK;
	}

	$stripId = $_POST[FORM_INTELLISTRIP_ID];
	$action = $_POST[FORM_ACTION];
	
	if( $action == ACTION_ALL_ON )
	{
		$powerState = 100;
	}
	else if( $action == ACTION_ALL_OFF )
	{
		$powerState = 0;
	}
	else
	{
		echo "<font color=\"#FF0000\"><b>Unrecognized action</b></font><BR />";
		//Shouldn't be any other action
		return TT_GENERAL;
	}
	
	$intellistrip = new Intellistrip($stripId);

	for( $i = 0; $i < INTELLISTRIP_NUMBER_PORTS; $i = $i + 1 )
	{
		$portId = $i + INTELLISTRIP_PORT_BASE;
		$rc = $intellistrip->setPortPower($portId, $powerState);
		if( TT_FAILED($rc) )
		{
			echo "<font color=\"#FF0000\"><b>Failed to set port power</b></font>";
			return $rc;
		}
	}
	return TT_OK;
}

function updateIntellistrips()
{
	updateSpecificPort();
	updateAction();
}

function displayPage()
{
	top("Main");
	
	updateIntellistrips();	
	/**
	echo "
		<style type=\"text/css\">
		<!--
		a:link {
			color: #0000EE;
		}
		a:visited {
			color: #0000EE;
		}
		-->
		</style>
		";
	*****/
	displayIntellistrips();

	bottom();
}

displayPage();
?>
<font color="#FF0000" size="+2"><b>